<?php

defined( '_PSPEXEC_' ) or die( 'Restricted access' );

    function pr($var) {
	    echo '<pre>';
	    print_r($var);
	    echo '</pre>';
    }

    function am() {
	    $r = array();
	    $args = func_get_args();
	    foreach ($args as $a) {
		    if (!is_array($a)) {
			    $a = array($a);
		    }
		    $r = array_merge($r, $a);
	    }
	    return $r;
    }
	
    function merge($arr1, $arr2 = null) {
		$args = func_get_args();
		$r = (array)current($args);
		while (($arg = next($args)) !== false) {
			foreach ((array)$arg as $key => $val)	 {
				if (!empty($r[$key]) && is_array($r[$key]) && is_array($val)) {
					$r[$key] = merge($r[$key], $val);
				} elseif (is_int($key)) {
					$r[] = $val;
				} else {
					$r[$key] = $val;
				}
			}
		}
		return $r;
	}
	
    function __Lang($singular, $args = null) {
	    if (!$singular) {
		    return;
	    }
	    if(!defined('__IsI18nOrL10n__')) {
	        Loader::import('pspframework.i18n.I18n');
            define('__IsI18nOrL10n__',true);
	    }
	    $translated = I18n::translate($singular);
	    if ($args === null) {
		    return $translated;
	    } elseif (!is_array($args)) {
		    $args = array_slice(func_get_args(), 1);
	    }
	    return vsprintf($translated, $args);
    }
	
    function filterEmptyVar($var) {
		if (is_array($var) && !empty($var) ) {
	        foreach($var as $key => $value){
	            if(empty($value)){
	                unset($var[$key]);
	            }
	 
	        }
	
		}

	    return $var;
	}
	
if (!function_exists('getMicrotime')) {
	function getMicrotime() {
		list($usec, $sec) = explode(' ', microtime());
		return ((float)$usec + (float)$sec);
	}
}


if(!function_exists('json_encode')) {
	function json_encode($arg) {
		$returnValue = '';
		$c           = '';
		$i           = '';
		$l           = '';
		$s           = '';
		$v           = '';
		$numeric     = true;

		switch (gettype($arg)) {
			case 'array':
				foreach ($arg AS $i => $v) {
					if (!is_numeric($i)) {
						$numeric = false;
						break;
					}
				}

				if ($numeric) {
					foreach ($arg AS $i => $v) {
						if (strlen($s) > 0) {
							$s .= ',';
						}
						$s .= json_encode($arg[$i]);
					}

					$returnValue = '[' . $s . ']';
				} else {
					foreach ($arg AS $i => $v) {
						if (strlen($s) > 0) {
							$s .= ',';
						}
						$s .= json_encode($i) . ':' . json_encode($arg[$i]);
					}

					$returnValue = '{' . $s . '}';
				}
				break;

			case 'object':
				foreach (get_object_vars($arg) AS $i => $v) {
					$v = json_encode($v);

					if (strlen($s) > 0) {
						$s .= ',';
					}
					$s .= json_encode($i) . ':' . $v;
				}

				$returnValue = '{' . $s . '}';
				break;

			case 'integer':
			case 'double':
				$returnValue = is_numeric($arg) ? (string) $arg : 'null';
				break;

			case 'string':
				$returnValue = '"' . strtr($arg, array(
					"\r"   => '\\r',    "\n"   => '\\n',    "\t"   => '\\t',     "\b"   => '\\b',
					"\f"   => '\\f',    '\\'   => '\\\\',   '"'    => '\"',
					"\x00" => '\u0000', "\x01" => '\u0001', "\x02" => '\u0002', "\x03" => '\u0003',
					"\x04" => '\u0004', "\x05" => '\u0005', "\x06" => '\u0006', "\x07" => '\u0007',
					"\x08" => '\b',     "\x0b" => '\u000b', "\x0c" => '\f',     "\x0e" => '\u000e',
					"\x0f" => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011', "\x12" => '\u0012',
					"\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016',
					"\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001a',
					"\x1b" => '\u001b', "\x1c" => '\u001c', "\x1d" => '\u001d', "\x1e" => '\u001e',
					"\x1f" => '\u001f'
				)) . '"';
				break;

			case 'boolean':
				$returnValue = $arg?'true':'false';
				break;

			default:
				$returnValue = 'null';
		}

		return $returnValue;
	}
}



/**
*生成目录
**/
    function get_dir($path){
		$newpath="";
        $path =str_replace("\\","/",$path);
	    $ROOT_PATH =str_replace("\\","/",APP_ROOT);
	    $detail=explode("/",$path);
	    foreach($detail AS $key=>$value){
		    if($value==''&&$key!=0){
			    //continue;
		    }
		    $newpath.="$value/";
		    if((preg_match("/^\//i",$newpath)||preg_match("/:/i",$newpath))&&!strstr($newpath,$ROOT_PATH)){continue;}
		    if( !is_dir($newpath) ){
			    if(substr($newpath,-1)=='\\'||substr($newpath,-1)=='/'){
				    $_newpath=substr($newpath,0,-1);
			    }else{
				    $_newpath=$newpath;
			    }
			    if(!is_dir($_newpath)&&!mkdir($_newpath)&&ereg("^\/",$ROOT_PATH)){
				    return 'false';
			    }
			    @chmod($newpath,0777);
		    }
	    }
	    return $path;
    }

	function LogError($message) {
		if (!class_exists('PspLog')) {
			Loader::import('pspframework.log.PspLog');
		}
		$bad = array("\n", "\r", "\t");
		$good = ' ';
		PspLog::write('error', str_replace($bad, $good, $message));
	}	
	
	function clearCache($params = null, $type = 'views', $ext = '.php') {
		if (is_string($params) || $params === null) {
			$params = preg_replace('/\/\//', '/', $params);
			$cache = TMP  . DS . 'cache' . DS . $type . DS . $params;

			if (is_file($cache . $ext)) {
				@unlink($cache . $ext);
				return true;
			} elseif (is_dir($cache)) {
				$files = glob($cache . '*');

				if ($files === false) {
					return false;
				}

				foreach ($files as $file) {
					if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
						@unlink($file);
					}
				}
				return true;
			} else {
				$cache = array(
					TMP  . DS . 'cache' . DS . $type . DS . '*' . $params . $ext,
					TMP  . DS . 'cache' . DS . $type . DS . '*' . $params . '_*' . $ext
				);
				$files = array();
				while ($search = array_shift($cache)) {
					$results = glob($search);
					if ($results !== false) {
						$files = array_merge($files, $results);
					}
				}
				if (empty($files)) {
					return false;
				}
				foreach ($files as $file) {
					if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
						@unlink($file);
					}
				}
				return true;
			}
		} elseif (is_array($params)) {
			foreach ($params as $file) {
				clearCache($file, $type, $ext);
			}
			return true;
		}
		return false;
	}

    function SystemDebug($querynum,$querytime) {

	    if (!defined("SYS_TIME_START")) {
		    return false;
	    }
	    if (!defined("SYS_MEMORY_START") || !function_exists("memory_get_usage")) {
		    return false;
	    }
	    $end_time = getMicrotime();
	    $time_used = round(($end_time -SYS_TIME_START), 5);
	    $mysql_count = $querynum;
	    $mysql_times = $querytime;
	    $end = memory_get_usage();
	    $used = $end -SYS_MEMORY_START;
	    if ($used <= 1024) {
		    $used = "1 KB";
	    } elseif ($used > 1024 && $used < (1024 * 1024)) {
		    $used = round(($used / 1024), 4) . " KB";
	    } else {
		    $used = round(($used / (1024 * 1024)), 4) . " MB";
	    }
	    $memory = $used;
	    $msg = "Pspframework Processed in " . $time_used . " second(s), mysql: " . $mysql_count . " queries";
	    $msg .= ", " . $mysql_times . " second(s)";
	    if ($memory) {
		    $msg .= ", memory: " . $memory;
	    }
       // if(!VERSION_INFO)return '';
	    return $msg;
    }

function sub_str($str, $length = 0, $append = true, $charset='utf8') {
	$str = trim($str);
	$strlength = strlen($str);
	$charset = strtolower($charset);

	if ($charset == 'utf8') {
		$l = 0;
		$i=0;
		while ($i < $strlength) {
			if (ord($str{$i}) < 0x80) { 
				$l++; $i++;
			} else if (ord($str{$i}) < 0xe0) {
				$l++; $i += 2; 
			} else if (ord($str{$i}) < 0xf0) { 
				$l += 2; $i += 3; 
			} else if (ord($str{$i}) < 0xf8) {
				$l += 1; $i += 4; 
			} else if (ord($str{$i}) < 0xfc) { 
				$l += 1; $i += 5; 
			} else if (ord($str{$i}) < 0xfe) { 
				$l += 1; $i += 6; 
			}

			if ($l >= $length) { 
				$newstr = substr($str, 0, $i);
				break;
			}
		}
		if($l < $length) {
			return $str;
		}
	} elseif($charset == 'gbk') {
		if ($length == 0 || $length >= $strlength) {
			return $str;
		}
		while ($i <= $strlength) {
			if (ord($str{$i}) > 0xa0) { 
				$l += 2; $i += 2;
			} else {
				$l++; $i++;
			}

			if ($l >= $length) { 
				$newstr = substr($str, 0, $i);
				break;
			}
		}
	}

	if ($append && $str != $newstr) {
		$newstr .= '..';
	}

	return $newstr;
}
?>